/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.boundary;

import it.polimi.meteocal.control.EventManager;
import it.polimi.meteocal.control.MessageManager;
import it.polimi.meteocal.control.WeatherService;
import it.polimi.meteocal.entity.Event;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author paolo
 *
 */
@ManagedBean
@ViewScoped
public class HomePageBean implements Serializable {

    private ScheduleModel eventModel;
    private ScheduleEvent event = new DefaultScheduleEvent();
    private String idselected;

    @EJB
    private EventManager em;
    @EJB
    private MessageManager mm;
    @EJB
    private WeatherService ws;

    public HomePageBean() {
    }

    public EventManager getEm() {
        return em;
    }

    public void setEm(EventManager em) {
        this.em = em;
    }

    public MessageManager getMm() {
        return mm;
    }

    public void setMm(MessageManager mm) {
        this.mm = mm;
    }

    public WeatherService getWs() {
        return ws;
    }

    public void setWs(WeatherService ws) {
        this.ws = ws;
    }

    /**
     * inizialize the schedule of the logged user
     */
    @PostConstruct
    public void init() {
        eventModel = new DefaultScheduleModel();
        DefaultScheduleEvent scheduleEvent;
        for (Event e : em.getUserEvent()) {
            GregorianCalendar cal = e.getEndDate();
            cal.set(Calendar.HOUR_OF_DAY, e.getEndHour().get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, e.getEndHour().get(Calendar.MINUTE));
            Date endDate = cal.getTime();
            cal = e.getStartDate();
            cal.set(Calendar.HOUR_OF_DAY, e.getStartHour().get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, e.getStartHour().get(Calendar.MINUTE));
            Date beginDate = e.getStartDate().getTime();
            scheduleEvent = new DefaultScheduleEvent(e.getName(), beginDate, endDate, e);
            if (e.getPublicEvent()) {
                if (e.getOutdoor()) {
                    scheduleEvent.setStyleClass("eventpuou");
                } else {
                    scheduleEvent.setStyleClass("eventpuin");
                }
            } else {
                if (e.getOutdoor()) {
                    scheduleEvent.setStyleClass("eventprou");
                } else {
                    scheduleEvent.setStyleClass("eventprin");
                }
            }

            eventModel.addEvent(scheduleEvent);
        }
    }

    public String getIdselected() {
        return idselected;
    }

    public void setIdselected(String idselected) {
        this.idselected = idselected;
    }

    public Date getRandomDate(Date base) {
        Calendar date = Calendar.getInstance();
        date.setTime(base);
        date.add(Calendar.DATE, ((int) (Math.random() * 30)) + 1);    //set random day of month

        return date.getTime();
    }

    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar.getTime();
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    private Calendar today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar;
    }

    private Date previousDay8Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
        t.set(Calendar.HOUR, 8);

        return t.getTime();
    }

    private Date previousDay11Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
        t.set(Calendar.HOUR, 11);

        return t.getTime();
    }

    private Date today1Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 1);

        return t.getTime();
    }

    private Date theDayAfter3Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 2);
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 3);

        return t.getTime();
    }

    private Date today6Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 6);

        return t.getTime();
    }

    private Date nextDay9Am() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.AM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
        t.set(Calendar.HOUR, 9);

        return t.getTime();
    }

    private Date nextDay11Am() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.AM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
        t.set(Calendar.HOUR, 11);

        return t.getTime();
    }

    private Date fourDaysLater3pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 4);
        t.set(Calendar.HOUR, 3);

        return t.getTime();
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }

    public void addEvent(ActionEvent actionEvent) {
        if (event.getId() == null) {
            eventModel.addEvent(event);
        } else {
            eventModel.updateEvent(event);
        }

        event = new DefaultScheduleEvent();
    }

    /**
     * when an event is selected on the schedule the selectede event identifier
     * is saved
     *
     * @param selectEvent
     */
    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
        Event e = (Event) event.getData();
        idselected = e.getEventID().toString();
        System.out.println("***selected event: " + e.getEventID() + " " + e.getName());
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    /**
     *
     * @return the event detail page according to the selected event
     */
    public String gotoDetail() {
        return ("eventdetail?faces-redirect=true&idevent=" + idselected);
    }

    /**
     * ser the color green if there isn't pending message red otherwise
     *
     * @return the color of the button according to the presence of new pending
     * message
     */
    public String colorButton() {
        if (mm.checkPending()) {
            gotNewMessages();
            return "red";
        } else {
            return "green";
        }
    }

    public void gotNewMessages() {
        FacesMessage message;
        message = new FacesMessage(FacesMessage.SEVERITY_INFO, "You ot an Email", "click on the button to see your message page");

        addMessage(message);
    }
}
